﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using EnvDTE;
using Aoxuelf.MDSTool.UI;

namespace Aoxuelf.MDSTool.Model
{
    public class MethodWriteInfo
    {
        private static DTE dte;

        public static DTE Dte
        {
            get 
            {
                if (dte == null)
                {
                    dte =  (DTE)MDSToolPackage.GetGlobalService(typeof(DTE));
                }

                return dte;
            }
        }

        public string MethodName
        {
            get;
            set;
        }

        public string Summary
        {
            get;
            set;
        }

        public string Remarks
        {
            get;
            set;
        }

        public string Author
        {
            get;
            set;
        }

        public bool IsTransaction
        {
            get;
            set;
        }

        private IList<SQLParamInfo> sqlParams = new List<SQLParamInfo>();
        public IList<SQLParamInfo> SQLParams
        {
            get { return sqlParams; }
            set { sqlParams = value; }
        }

        private IList<ColumnInfo> columns = new List<ColumnInfo>();
        public IList<ColumnInfo> Columns
        {
            get { return columns; }
            set { columns = value; }
        }

        public override string ToString()
        {
            return "Method : " + this.MethodName;
        }

        public virtual string GenerateMethodString()
        {
            return string.Empty;
        }

        public void WriteToVS()
        {
            try
            {
                string text = GenerateMethodString();

                TextDocument txt = Dte.ActiveDocument.Object("TextDocument") as TextDocument;
                EditPoint ep = txt.Selection.ActivePoint.CreateEditPoint();
                EditPoint sp = txt.Selection.ActivePoint.CreateEditPoint();

                if (!txt.Selection.IsEmpty)
                {
                    txt.Selection.Delete(-1);
                }

                ep.Insert(text);
                sp.SmartFormat(ep);
            }
            catch (Exception ex)
            {
                MDSOutPaneWindow.MDSTool.WriteLine(string.Empty);
                MDSOutPaneWindow.MDSTool.WriteLine("异常：" + ex.Message);
                if (ex.StackTrace != null)
                {
                    MDSOutPaneWindow.MDSTool.WriteLine(ex.StackTrace);
                }
                MDSOutPaneWindow.MDSTool.Activate();
            }
        }

    }
}
